//
//  FamilyMemberBirthdayViewController.m
//  FD
//
//  Created by Yasin on 2018/10/17.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
// 添加 家庭成员生日列表也买

#import "FamilyMemberBirthdayViewController.h"
//view-cell
#import "BirthdayCell.h"
//controllr
#import "AddBirthViewController.h"
#import "FamilyEditViewController.h" //修改成员生日
//model
#import "FamilyModel.h"


@interface FamilyMemberBirthdayViewController ()<UITableViewDelegate,UITableViewDataSource>

/**uitable*/
@property (nonatomic,  strong) UITableView *tv;

/**数据源数组*/
@property (nonatomic,  strong) NSMutableArray  *dataArr;
/**表尾*/
@property (nonatomic,  strong) UIView *footerView;

@end

@implementation FamilyMemberBirthdayViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tv.frame = self.view.bounds;
    [self.view addSubview:self.tv];
    self.tv.tableFooterView = self.footerView;
    
    self.view.backgroundColor = kMainLightGrayColor; //sbsbsbsbsbssbsbsbsbsbsbsbsb
    self.tv.backgroundColor = kMainLightGrayColor;
    //请求数据
    //    [self initWithDataArr];
    
    //国际化
    self.navigationItem.title = NSLocalizedString(FamilyMemberBirthday, FamilyMem@"家庭成员生日"berBirthday);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self loadFamilyListData];
}

- (void)initWithDataArr {
    self.dataArr = @[@{@"name":@"爸爸",@"birthday":@"1968-10-8"},
                     @{@"name":@"张三",@"birthday":@"1968-10-8"}
                     ].mutableCopy;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BirthdayCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BirthdayCell"];
    FamilyModel *model = self.dataArr[indexPath.row];
    cell.nameLb.text = model.memberName;
    cell.birthdayLb.text = model.birthdayStr;
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    FamilyModel *model = self.dataArr[indexPath.row];
    
    FamilyEditViewController *editVC = [FamilyEditViewController new];
    editVC.model = model;

    [self.navigationController pushViewController:editVC animated:YES];
}


#pragma mark - 请求数据
- (void)loadFamilyListData {
    
    [MBProgressHUD fd_showHUDToView:self.view];
    
    
    [FDHttpAPi requestUserFamilListSuccessBlock:^(id responseObjc) {
        [MBProgressHUD fd_hideHUDFromView:self.view];
        NSString *event = responseObjc[@"event"];
        if ([event isEqualToString:SUCCESS]) {
            NSArray *data = responseObjc[@"data"];
            
            //清空之前数据
            [self.dataArr removeAllObjects];
            
            //解析数据
            for (NSDictionary *dic in data) {
                FamilyModel *model = [FamilyModel mj_objectWithKeyValues:dic];
                [self.dataArr addObject:model];
            }
            
            //刷新
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tv reloadData];
            });
            
        }else { //toast
            [JRToast showWithText:responseObjc[describe]];
        }
    } failBlock:^(NSError *error) {
        [MBProgressHUD fd_hideHUDFromView:self.view];
        FDLog(@"%@",[error localizedDescription]);
    }];
}

#pragma mark - getter 放最后
- (UITableView *)tv {
    if (!_tv) {
        _tv = [[UITableView alloc] initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tv.delegate = self;
        _tv.dataSource = self;
        //注册cell、
        [_tv registerClass:[BirthdayCell class] forCellReuseIdentifier:@"BirthdayCell"];
    }
    return _tv;
}

- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
- (UIView *)footerView
{
    if (!_footerView) {
        _footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 110)];
//        _footerView.backgroundColor = rgba(245, 245, 245, 1.0);

        //国际化
        NSString *addStr = NSLocalizedString(AddFamilyMemberBirthday, @"添加家庭成员生日");
        UIButton *addBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [addBtn setTitle:addStr forState:(UIControlStateNormal)];
        [addBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
        addBtn.backgroundColor = kMainRedColor;
        [addBtn addTarget:self action:@selector(clickAddBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
        addBtn.frame = CGRectMake(20, 50, SCREEN_WIDTH - 40, 40);
        addBtn.layer.masksToBounds = YES;
        addBtn.layer.cornerRadius = 5;
        
        //添加
        [_footerView addSubview:addBtn];
    }
    return _footerView;
}

- (void)clickAddBtnAction:(UIButton *)btn
{
    NSLog(@"%s---%d",__func__,__LINE__);
    
    AddBirthViewController *addVC = [AddBirthViewController new];
    [self.navigationController pushViewController:addVC animated:YES];
}


@end
